#!/bin/sh
# Mocks are called indirectly by the sourced nvm functions.
# shellcheck disable=SC2329

cleanup() {
  unset -f nvm_download row node_index iojs_index header_only_index filtered_index mixed_case_index
  unset NVM_LTS NVM_ONLY NVM_ONLY_SELECTED NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR
  case "${TEST_DIR-}" in
    /*) rm -rf "${TEST_DIR}" ;;
  esac
}
die () { echo "$@" ; cleanup ; exit 1; }

TEST_DIR="$(mktemp -d)" || die 'unable to create a temp dir'
NVM_DIR="${TEST_DIR}/node"
export NVM_DIR
mkdir -p "${NVM_DIR}" "${TEST_DIR}/iojs" "${TEST_DIR}/empty" "${TEST_DIR}/filtered" "${TEST_DIR}/mixed" \
  || die 'unable to create NVM_DIR'

: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh

unset NVM_LTS NVM_ONLY NVM_ONLY_SELECTED
NVM_NODEJS_ORG_MIRROR='https://mirror.example/node'
NVM_IOJS_ORG_MIRROR='https://mirror.example/iojs'
export NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR

row() {
  printf '%s\t2026-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\t%s\t-\n' "$1" "$2"
}

# newest first, like the real index; the first line is always dropped as the header,
# even when it looks like a version
node_index() {
  row v99.0.0 -
  row v27.0.0 -
  row v27.0.0-alpha.1.2.3 -
  row v26.0.0 -
  row v26.0.0-rc.2 -
  row v25.0.0-1-x -
  row v1.2.3junk -
  row junkv1.2.3 -
  row v24.2.0 Krypton
  row v24.1.0 Krypton
  row v22.10.0 Jod
  row v22.9.0 Jod
  row v21.0.0 'B@d'
  row v20.111.1 Iron
  row v20.1.1 Iron
  row v1.0.0 .hidden
  row v1.0.1 _under
}

iojs_index() {
  printf 'version\tdate\tfiles\tnpm\tv8\tuv\tzlib\topenssl\tmodules\n'
  printf 'v2.0.0\t2015-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\n'
  printf 'v1.2.0\t2015-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\n'
  printf 'v1.1.0\t2015-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\n'
}

header_only_index() {
  row version lts
}

filtered_index() {
  row version lts
  row junkv1.2.3 Krypton
  row v1.2.3junk Krypton
  row v24.2.0 .hidden
}

mixed_case_index() {
  row version lts
  row v24.2.0 Krypton
  row v24.1.0 krypton
  row v22.0.0 Jod
}

FIXTURE=node_index
nvm_download() {
  printf '%s\n' "$*" > "${TEST_DIR}/download-args"
  "${FIXTURE}"
}

ALL_NODE='v20.1.1 Iron
v20.111.1 Iron *
v22.9.0 Jod
v22.10.0 Jod *
v24.1.0 Krypton
v24.2.0 Krypton *
v25.0.0-1-x
v26.0.0-rc.2
v26.0.0
v27.0.0'

# fewer than three arguments
for COUNT in 0 1 2; do
  case "${COUNT}" in
    0) set -- ;;
    1) set -- node ;;
    2) set -- node std ;;
  esac
  try nvm_ls_remote_index_tab "$@"
  [ "_${CAPTURED_EXIT_CODE}" = '_5' ] || die "${COUNT} args: expected exit 5, got >${CAPTURED_EXIT_CODE}<"
  [ "_${CAPTURED_STDOUT}" = '_' ] || die "${COUNT} args: expected no stdout, got >${CAPTURED_STDOUT}<"
  try_err nvm_ls_remote_index_tab "$@"
  [ "_${CAPTURED_STDERR}" = '_not enough arguments' ] || die "${COUNT} args: got >${CAPTURED_STDERR}<, expected >not enough arguments<"
done
set --

# no mirror for the flavor/type, or an invalid mirror: exit 3 without downloading
for ARGS in 'foo std' 'node nightly' 'iojs nightly'; do
  rm -f "${TEST_DIR}/download-args"
  try nvm_ls_remote_index_tab "${ARGS% *}" "${ARGS#* }" ''
  [ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "${ARGS}: expected exit 3, got >${CAPTURED_EXIT_CODE}<"
  [ "_${CAPTURED_STDOUT}" = '_' ] || die "${ARGS}: expected no stdout, got >${CAPTURED_STDOUT}<"
  try_err nvm_ls_remote_index_tab "${ARGS% *}" "${ARGS#* }" ''
  [ "_${CAPTURED_STDERR}" = '_unknown type of node.js or io.js release' ] || die "${ARGS}: got >${CAPTURED_STDERR}<"
  [ ! -e "${TEST_DIR}/download-args" ] || die "${ARGS}: downloaded without a mirror"
done

NVM_NODEJS_ORG_MIRROR="https://mirror.example/no'de"
try nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "invalid mirror: expected exit 3, got >${CAPTURED_EXIT_CODE}<"
[ "_${CAPTURED_STDOUT}" = '_' ] || die "invalid mirror: expected no stdout, got >${CAPTURED_STDOUT}<"
try_err nvm_ls_remote_index_tab node std ''
# shellcheck disable=SC2016
[ "_${CAPTURED_STDERR}" = '_$NVM_NODEJS_ORG_MIRROR and $NVM_IOJS_ORG_MIRROR may only contain a URL' ] \
  || die "invalid mirror: got >${CAPTURED_STDERR}<"
[ ! -e "${TEST_DIR}/download-args" ] || die 'invalid mirror: downloaded anyway'

# download failure; credentials in the mirror are redacted
NVM_NODEJS_ORG_MIRROR='https://u:p@mirror.example/node'
nvm_download() { return 7; }
try nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "failed download: expected exit 3, got >${CAPTURED_EXIT_CODE}<"
[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "failed download: got >${CAPTURED_STDOUT}<, expected >N/A<"
try_err nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_STDERR}" = '_Failed to download version list from https://***@mirror.example/node/index.tab' ] \
  || die "failed download: got >${CAPTURED_STDERR}<"
NVM_NODEJS_ORG_MIRROR='https://mirror.example/node'
nvm_download() {
  printf '%s\n' "$*" > "${TEST_DIR}/download-args"
  "${FIXTURE}"
}

# node: all non-alpha rows, numerically sorted, the newest of each LTS line starred
try nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_EXIT_CODE}" = '_0' ] || die "node: expected exit 0, got >${CAPTURED_EXIT_CODE}<"
[ "_${CAPTURED_STDOUT}" = "_${ALL_NODE}" ] || die "node: got >${CAPTURED_STDOUT}<, expected >${ALL_NODE}<"
[ "_$(cat "${TEST_DIR}/download-args")" = '_-L -s https://mirror.example/node/index.tab -o -' ] \
  || die "node: wrong download args >$(cat "${TEST_DIR}/download-args")<"
try_err nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_STDERR}" = '_' ] || die "node: expected no stderr, got >${CAPTURED_STDERR}<"

# side effects: LTS aliases, and the recorded LTS status
[ "_$(cat "${NVM_DIR}/alias/lts/*")" = '_lts/krypton' ] || die "lts/* alias: got >$(cat "${NVM_DIR}/alias/lts/*")<"
[ "_$(cat "${NVM_DIR}/alias/lts/krypton")" = '_v24.2.0' ] || die "lts/krypton alias: got >$(cat "${NVM_DIR}/alias/lts/krypton")<"
[ "_$(cat "${NVM_DIR}/alias/lts/jod")" = '_v22.10.0' ] || die "lts/jod alias: got >$(cat "${NVM_DIR}/alias/lts/jod")<"
[ "_$(cat "${NVM_DIR}/alias/lts/iron")" = '_v20.111.1' ] || die "lts/iron alias: got >$(cat "${NVM_DIR}/alias/lts/iron")<"
ALIASES="$(cd "${NVM_DIR}/alias/lts" && LC_ALL=C command ls)"
EXPECTED='*
iron
jod
krypton'
[ "_${ALIASES}" = "_${EXPECTED}" ] || die "lts aliases: got >${ALIASES}<, expected >${EXPECTED}<"
nvm_grep -qx 'v24.2.0 krypton' "${NVM_DIR}/.lts-status" || die 'node: LTS status was not recorded'

# patterns
for CASE in \
  '*|ALL' \
  '22.10.0.|v22.10.0 Jod *' \
  '20.1.1|v20.1.1 Iron' \
  'v20.1|v20.1.1 Iron' \
  '26.0.0-rc|v26.0.0-rc.2' \
  '25.0.0-1|v25.0.0-1-x' \
  '27.0.0|v27.0.0' \
  'iojs-v20.1.1|N/A' \
  '99|N/A' \
; do
  PATTERN="${CASE%%|*}"
  EXPECTED="${CASE#*|}"
  [ "${EXPECTED}" = 'ALL' ] && EXPECTED="${ALL_NODE}"
  EXPECTED_CODE=0
  [ "${EXPECTED}" = 'N/A' ] && EXPECTED_CODE=3
  try nvm_ls_remote_index_tab node std "${PATTERN}"
  [ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "pattern ${PATTERN}: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
  [ "_${CAPTURED_EXIT_CODE}" = "_${EXPECTED_CODE}" ] || die "pattern ${PATTERN}: got exit >${CAPTURED_EXIT_CODE}<, expected >${EXPECTED_CODE}<"
done

# NVM_ONLY / NVM_ONLY_SELECTED categories
for CASE in \
  '|alpha|v27.0.0-alpha.1.2.3' \
  '|non-lts|v25.0.0-1-x
v26.0.0-rc.2
v26.0.0
v27.0.0' \
  '|lts|v20.1.1 Iron
v20.111.1 Iron *
v22.9.0 Jod
v22.10.0 Jod *
v24.1.0 Krypton
v24.2.0 Krypton *' \
  '||ALL' \
  '|iojs|N/A' \
  'alpha|lts|v27.0.0-alpha.1.2.3' \
; do
  NVM_ONLY_SELECTED="${CASE%%|*}"
  REST="${CASE#*|}"
  NVM_ONLY="${REST%%|*}"
  EXPECTED="${REST#*|}"
  [ "${EXPECTED}" = 'ALL' ] && EXPECTED="${ALL_NODE}"
  try nvm_ls_remote_index_tab node std ''
  [ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "NVM_ONLY_SELECTED=${NVM_ONLY_SELECTED} NVM_ONLY=${NVM_ONLY}: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
done

# a release pattern excludes that release's alphas; a shorter one does not
NVM_ONLY_SELECTED=''
NVM_ONLY='alpha,non-lts'
try nvm_ls_remote_index_tab node std '27.0.0'
[ "_${CAPTURED_STDOUT}" = '_v27.0.0' ] || die "alpha release pattern: got >${CAPTURED_STDOUT}<"
EXPECTED='v27.0.0-alpha.1.2.3
v27.0.0'
try nvm_ls_remote_index_tab node std '27'
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "alpha major pattern: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
unset NVM_ONLY NVM_ONLY_SELECTED

# NVM_LTS
for CASE in \
  '*|v20.1.1 Iron
v20.111.1 Iron *
v22.9.0 Jod
v22.10.0 Jod *
v24.1.0 Krypton
v24.2.0 Krypton *' \
  'jod|v22.9.0 Jod
v22.10.0 Jod *' \
  '-1|v22.9.0 Jod
v22.10.0 Jod *' \
  'argon|N/A' \
; do
  NVM_LTS="${CASE%%|*}"
  EXPECTED="${CASE#*|}"
  EXPECTED_CODE=0
  [ "${EXPECTED}" = 'N/A' ] && EXPECTED_CODE=3
  try nvm_ls_remote_index_tab node std ''
  [ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "NVM_LTS=${NVM_LTS}: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
  [ "_${CAPTURED_EXIT_CODE}" = "_${EXPECTED_CODE}" ] || die "NVM_LTS=${NVM_LTS}: got exit >${CAPTURED_EXIT_CODE}<, expected >${EXPECTED_CODE}<"
done

NVM_LTS='Jod'
try nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "uppercase NVM_LTS: expected exit 3, got >${CAPTURED_EXIT_CODE}<"
[ "_${CAPTURED_STDOUT}" = '_' ] || die "uppercase NVM_LTS: expected no stdout, got >${CAPTURED_STDOUT}<"
try_err nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_STDERR}" = '_LTS names must be lowercase' ] || die "uppercase NVM_LTS: got >${CAPTURED_STDERR}<"

NVM_LTS='-9'
try nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_EXIT_CODE}" = '_2' ] || die "NVM_LTS=-9: expected exit 2, got >${CAPTURED_EXIT_CODE}<"
[ "_${CAPTURED_STDOUT}" = '_' ] || die "NVM_LTS=-9: expected no stdout, got >${CAPTURED_STDOUT}<"
try_err nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_STDERR}" = '_That many LTS releases do not exist yet.' ] || die "NVM_LTS=-9: got >${CAPTURED_STDERR}<"
unset NVM_LTS

# an index with no usable rows
for CASE in 'empty|header_only_index' 'filtered|filtered_index'; do
  NVM_DIR="${TEST_DIR}/${CASE%%|*}"
  FIXTURE="${CASE#*|}"
  try nvm_ls_remote_index_tab node std ''
  [ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "${FIXTURE}: expected exit 3, got >${CAPTURED_EXIT_CODE}<"
  [ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "${FIXTURE}: got >${CAPTURED_STDOUT}<, expected >N/A<"
  [ "_$(command ls -A "${NVM_DIR}/alias/lts")" = '_' ] || die "${FIXTURE}: LTS aliases were written"
  [ ! -e "${NVM_DIR}/.lts-status" ] || die "${FIXTURE}: LTS status was recorded"
done

# LTS aliases are deduplicated case-insensitively: the first row of a codename wins
NVM_DIR="${TEST_DIR}/mixed"
FIXTURE=mixed_case_index
try nvm_ls_remote_index_tab node std ''
[ "_${CAPTURED_EXIT_CODE}" = '_0' ] || die "mixed case: expected exit 0, got >${CAPTURED_EXIT_CODE}<"
[ "_$(cat "${NVM_DIR}/alias/lts/krypton")" = '_v24.2.0' ] || die "mixed case lts/krypton alias: got >$(cat "${NVM_DIR}/alias/lts/krypton")<"
[ "_$(cat "${NVM_DIR}/alias/lts/jod")" = '_v22.0.0' ] || die "mixed case lts/jod alias: got >$(cat "${NVM_DIR}/alias/lts/jod")<"
# whether the older krypton row is also starred is left unasserted
LINES="$(nvm_echo "${CAPTURED_STDOUT}" | command sed -n '$=')"
[ "_${LINES}" = '_3' ] || die "mixed case: expected 3 lines, got >${CAPTURED_STDOUT}<"
LINE="$(nvm_echo "${CAPTURED_STDOUT}" | command sed -n 1p)"
[ "_${LINE}" = '_v22.0.0 Jod *' ] || die "mixed case line 1: got >${LINE}<, expected >v22.0.0 Jod *<"
LINE="$(nvm_echo "${CAPTURED_STDOUT}" | command sed -n 2p)"
case "${LINE}" in
  'v24.1.0 krypton' | 'v24.1.0 krypton *') ;;
  *) die "mixed case line 2: got >${LINE}<" ;;
esac
LINE="$(nvm_echo "${CAPTURED_STDOUT}" | command sed -n 3p)"
[ "_${LINE}" = '_v24.2.0 Krypton *' ] || die "mixed case line 3: got >${LINE}<, expected >v24.2.0 Krypton *<"

# io.js: prefixed, sorted oldest first, no LTS aliases or status
NVM_DIR="${TEST_DIR}/iojs"
FIXTURE=iojs_index
EXPECTED='iojs-v1.1.0
iojs-v1.2.0
iojs-v2.0.0'
try nvm_ls_remote_index_tab iojs std ''
[ "_${CAPTURED_EXIT_CODE}" = '_0' ] || die "iojs: expected exit 0, got >${CAPTURED_EXIT_CODE}<"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "iojs: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
[ "_$(cat "${TEST_DIR}/download-args")" = '_-L -s https://mirror.example/iojs/index.tab -o -' ] \
  || die "iojs: wrong download args >$(cat "${TEST_DIR}/download-args")<"
[ -d "${NVM_DIR}/alias/lts" ] || die 'iojs: alias/lts dir was not created'
[ "_$(command ls -A "${NVM_DIR}/alias/lts")" = '_' ] || die 'iojs: LTS aliases were written'
[ ! -e "${NVM_DIR}/.lts-status" ] || die 'iojs: LTS status was recorded'

for CASE in \
  'iojs-v1.1|iojs-v1.1.0' \
  '2.0|iojs-v2.0.0' \
  'iojs-|iojs-v1.1.0
iojs-v1.2.0
iojs-v2.0.0' \
; do
  PATTERN="${CASE%%|*}"
  EXPECTED="${CASE#*|}"
  try nvm_ls_remote_index_tab iojs std "${PATTERN}"
  [ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "iojs pattern ${PATTERN}: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
done

NVM_ONLY='iojs'
try nvm_ls_remote_index_tab iojs std '1.2'
[ "_${CAPTURED_STDOUT}" = '_iojs-v1.2.0' ] || die "iojs NVM_ONLY=iojs: got >${CAPTURED_STDOUT}<, expected >iojs-v1.2.0<"
NVM_ONLY='lts,non-lts'
try nvm_ls_remote_index_tab iojs std ''
[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "iojs NVM_ONLY=lts,non-lts: got >${CAPTURED_STDOUT}<, expected >N/A<"
unset NVM_ONLY

NVM_LTS='*'
try nvm_ls_remote_index_tab iojs std ''
[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "iojs NVM_LTS: got >${CAPTURED_STDOUT}<, expected >N/A<"
[ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "iojs NVM_LTS: expected exit 3, got >${CAPTURED_EXIT_CODE}<"
unset NVM_LTS

cleanup
